// test_frame.cpp : Defines the entry point for the console application.
//

#include "test_frame.h"

CTestFrame::CTestFrame()
{

}

CTestFrame::~CTestFrame()
{
}

LRESULT CTestFrame::MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	return CWin32App::MsgProc( hWnd, msg, wParam, lParam );
}

void CTestFrame::Active()
{
	CEngine* engine = GetEngine();
	engine->Update();
	engine->Render();
}

BOOL CTestFrame::CreateFrame(const char* mediaPath, DWORD width, DWORD height, RENDER_DEVICE_TYPE type)
{
	if (!Create(GetModuleHandle(NULL), "Test Frame", width, height))
		return FALSE;

	if (!CreateEngine(type, (void*)GetWindowHandle()))
		return FALSE;

	CEngine* engine = GetEngine();
	CFileSystem* fileSystem = engine->GetFileSystem();
	CSceneManager* sceneManager = engine->GetSceneManager();
	fileSystem->SetRoot(mediaPath);

	_camera = new CCamera;
	_camera->SetPosition(D3DXVECTOR3(0, 300, -200));
	_camera->SetLookDirection(D3DXVECTOR3(0, -1, 1));
	_camera->SetUpDirection(D3DXVECTOR3(0, 1, 0));
	engine->SetCurrentCamera(_camera);

	_scene = sceneManager->CreateScene("TestFrameScene");
	_scene->AddRef();

	CRenderWindow* renderWindow = engine->CreateRenderWindow(GetWindowHandle());
	renderWindow->MakeDefaultRenderSteps();
	CRenderStepViewCulling* renderStepViewCulling = (CRenderStepViewCulling*)renderWindow->GetRenderStepByName(CRenderStepViewCulling::RenderStepName);
	CRenderStepSceneRender* renderStepSceneRender = (CRenderStepSceneRender*)renderWindow->GetRenderStepByName(CRenderStepSceneRender::RenderStepName);
	renderStepViewCulling->SetCamera(_camera);
	CCullingProcessorDefault* cullingProcessor = new CCullingProcessorDefault;
	renderStepViewCulling->SetCullingProcessor(cullingProcessor);
	cullingProcessor->Release();
	renderStepViewCulling->SetScene(_scene);
	renderStepSceneRender->SetClearParam(TRUE, TRUE, TRUE, 0xff808080, 1.0f, 0);

	engine->SetCurrentRenderWindow(renderWindow);

	if (!CreateScene())
		return FALSE;

	return TRUE;
}

void CTestFrame::DestroyFrame()
{
	SAFE_RELEASE(_camera);
	SAFE_RELEASE(_scene);
	DestroyEngine();
	Destroy();
}
